<?php
use services\models\Goods;
use yii\helpers\ArrayHelper;

$this->context->bodyClass = 'page-order';
/** @var \services\models\Order $order */
$errs = $order->getErrors();

$array = ArrayHelper::toArray($order->productsInCart);
$number = array_sum(array_column($array, 'number'));

$cart_ids = implode(',',$order->cart_ids);

$uid = !empty($memberInfo) ? $memberInfo->uid : 0;
?>
<header>
    <div class="header-left" id="back"><span class="icon icon-arrow-left"></span></div>
    <h1>确认下单</h1>
    <div class="header-right" onclick="$.go('<?=Yii::$app->urlManager->createUrl(['index'])?>');" ><span class="icon icon-home"></span></div>
</header>
<section class="order-info">
	<div class="order-address" id="addressSelect" uid="<?= $uid;?>">
    <?php if($memberInfo):?>
        <p><i><?=$memberInfo->consignee?></i><i><?=$memberInfo->cell?></i><br><i><?=$memberInfo->s1?><?=$memberInfo->s2?><?=$memberInfo->s3?><?=$memberInfo->address?></i></p>
    <?php else :?>
        <p class="order-address-empty">为了快递能更快更准确的给您派件，需要您重新填写收货地址，给您造成的不便敬请谅解</p>
    <?php endif ;?>
	<span class="icon icon-arrow-right"></span>
	</div>
    <div class="order-goods">
        <h2>商品清单</h2>
        <div class="order-goods-view"><a href="<?=Yii::$app->urlManager->createUrl(['order/order-goods','cart_ids'=>$cart_ids, '_csrf'=>Yii::$app->request->csrfToken]);?>">查看全部<i><?= $number;//count($order->productsInCart) ?></i>件商品</a>
            <span class="icon icon-arrow-right"></span>
        </div>
        <ul class="order-goods-thumb box">
            <?php $i=0;foreach ($order->productsInCart as $od): ++$i;if($i>4)break;
                $goodPic = Goods::findByGoodsIdFromRedis($od->goods_id, ['pic']);
                ?>
                <li>
                    <div>
                        <img src="<?=Yii::$app->params['goods_image_url'].str_replace('./upload', 'upload', $goodPic['pic'])?>">
                    </div>
                    x<?= $od->number ?>
                </li>
            <?php endforeach; ?>
        </ul>
    </div>
    <ul class="order-sum">
        <li class="box">
            <div class="list-name">优惠金额</div>
            <div class="list-value flex1 red">¥﹣<?=$order->reduce_amount?></div>
        </li>
        <li class="box">
            <div class="list-name">商品金额</div>
            <div class="list-value flex1 red">￥<?= $order->goods_amount ?>+<?= $order->try_coin ?>试豆</div>
        </li>
        <li class="box">
            <div class="list-name">邮<i></i>费</div>
            <div class="list-value flex1 red">¥<i id="expressCharge"><?= $order->ship_amount_discount ?></i></div>
        </li>
    </ul>
    <?php
    #var_dump($order);
    if($order->try_coin < $order->score && $order->ship_amount_discount >0 ):
    ?>
    <div class="order-deduct"><span id="orderDeduct"><span class="switch"></span><span>用<i><?= $order->ship_amount * $order->money_convery_to_money ?></i>试豆抵扣邮费</span></span><span class="error"></span>
    </div>
    <?php endif;?>
    <div class="order-pay">
        <h2 <?php if($member_money!=0) echo 'id="payByBalance"';?> class="box">
            <div class="list-name flex1">支付方式</div>
            <div class="list-value flex1"><span>在线支付</span><?php if($member_money!=0):?><span class="icon icon-arrow-down"></span><?php endif;?></div>
        </h2>
        <div class="order-pay-inner">
            <div class="order-pay-cont box">
                <div class="order-pay-input"><input type="number" id="orderPayBalance" placeholder="余额支付"></div>
                <div class="order-balance flex1 ellipsis red">目前余额：￥<i id="balance"><?=$member_money?></i></div>
                <div class="order-pay-confirm" id="orderPayConfirm">确认</div>
            </div>
            <div class="error red" id="balanceError"></div>
        </div>
    </div>
</section>
<div class="order-settle box-sizing" id="orderSettle">
    <p class="ellipsis">合计:<i class="order-settle-amount red">￥<i id="settleAmount"><?=$order->order_amount?></i>+<i id="settleSD"><?= $order->try_coin ?></i>试豆</i></p>
    <?php if($order->ship_amount_discount):?>
    <p>（含运费<i id="settleExpressCharge">￥<?=$order->ship_amount_discount?></i>）</p>
    <?php endif;?>
    <div class="order-submit" id="orderSubmit">立即下单</i></div>
</div>

<?php $this->beginBlock('js');?>
<script src="<?= Yii::getAlias('@static') ?>/wap/common/js/zepto.min.js"></script>
<script src="<?= Yii::getAlias('@static') ?>/wap/common/js/fastclick.min.js"></script>
<script src="<?= Yii::getAlias('@static') ?>/wap/common/js/king.wap.js"></script>
<script src="<?= Yii::getAlias('@static') ?>/wap/liketry/js/order.js"></script>
<script>
    <?php if(key_exists('fatal', $errs)):?>
    $.alert(['<?=$errs['fatal'][0]?>', '返回购物车'], function(){window.location.href='<?=Yii::$app->urlManager->createUrl('cart')?>';return false;});
    <?php endif;?>
	
	var backUrl = '<?=Yii::$app->urlManager->createUrl(['cart'])?>';
	var addressListUrl = '<?=Yii::$app->urlManager->createUrl(["member-address/listorder",'uid'=>$uid,'cart_ids'=>$cart_ids]);?>';
	
    var getCellCodeUrl = '<?=Yii::$app->urlManager->createUrl(["member-info/cell-get-code",'cell'=>'']);?>';
	
    var checkCellCodeUrl = '<?=Yii::$app->urlManager->createUrl(["member-info/cell-confirm",'cell'=>'']);?>';
	
	var getCellCodeForBalancePayUrl = '<?=Yii::$app->urlManager->createUrl(["order/member-money-sms"]);?>';
	
	var checkCellCodeForBalancePayUrl = '<?=Yii::$app->urlManager->createUrl(["order/member-money-sms-check"]);?>';
	
	var sdDeductUrl = '<?=Yii::$app->urlManager->createUrl(["order/validate"]);?>';
	
	var settleOrderUrl = '<?=Yii::$app->urlManager->createUrl(["order/save"]);?>';
	
	var settleSuccessUrl = '<?=Yii::$app->urlManager->createUrl(["order/success",'order_info_id'=>'']);?>';
	
	var settleSuccessPayedUrl = '<?=Yii::$app->urlManager->createUrl(["order/payed",'order_info_id'=>'']);?>';
	
	var orderData = {cart_ids:'<?=$cart_ids?>', member_address_id:'<?= $uid?>', use_coin_for_ship_amount:0, member_money:0, cell: '<?= !empty($memberInfo) ? $memberInfo->cell : '';?>'};
	
	var userInfo = {balance: '<?=$member_money?>', isBind: <?=$cellConfirm?'true':'false'?>, validate: false, expressCharge: '<?= $order->ship_amount_discount ?>', settleAmount: '<?=$order->order_amount?>', try_coin:'<?= $order->try_coin ?>'};
	
	var validateHtml1 = '<div class="cell-validate"><p class="validate-tips">为了您的账号安全请绑定手机号</p><div class="validate-input box"><div class="flex1"><input type="tel" maxlength="11" id="validateCell" placeholder="输入手机号码"></div></div><div class="validate-input box"><div class="flex1"><input type="text" maxlength="6" id="cellCode" name="code" placeholder="输入验证码"></div><div id="getcode" class="validate-btn">获取验证码</div></div><p class="error" id="validateError"></p></div>';
	
	var validateHtml2 = '<div class="cell-validate"><p class="validate-tips">您使用了虚拟财产，为了您的账号安全请绑定手机号进行安全验证</p><div class="validate-input box"><div class="flex1"><input type="tel" maxlength="11" id="validateCell" placeholder="输入手机号码"></div></div><div class="validate-input box"><div class="flex1"><input type="text" maxlength="6" id="cellCode" name="code" placeholder="输入验证码"></div><div id="getcode" class="validate-btn">获取验证码</div></div><p class="error" id="validateError"></p></div>';
	
	var validateHtml3 = '<div class="cell-validate"><p class="validate-tips">您使用了虚拟财产，请输入验证码进行安全验证</p><div class="validate-input box"><div class="flex1"><input type="tel" maxlength="11" readonly value="<?= !empty($order->memberInfoBestry->memberInfo->cell) ? $order->memberInfoBestry->memberInfo->cell : '';?>" id="validateCell" placeholder="输入手机号码"></div></div><div class="validate-input box"><div class="flex1"><input type="text" maxlength="6" id="cellCode" name="code" placeholder="输入验证码"></div><div id="getValidateCode" class="validate-btn">获取验证码</div></div><p class="error" id="validateError"></p></div>';
</script>
<?php $this->endBlock();?>
<?php $this->beginBlock('css');?>
    <link rel="stylesheet" type="text/css" href="<?= Yii::getAlias('@static')?>/wap/liketry/css/cart.css">
<?php $this->endBlock();?>